package top.jolyoulu.mytomcat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author LuZhouJin
 * @Date 2023/8/4
 * 自定义的Request 基于HTTP协议
 */
public class MyRequest {

    private ChannelHandlerContext ctx;
    private HttpRequest req;

    public MyRequest(ChannelHandlerContext ctx,HttpRequest req) {
        this.ctx = ctx;
        this.req = req;
    }

    public String getMethod() {
        return req.method().name();
    }

    public String getUrl() {
        return req.uri();
    }

    public Map<String, List<String>> getParameters(){
        QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
        return decoder.parameters();
    }

    public String getParameter(String name){
        Map<String, List<String>> params = getParameters();
        List<String> param = params.get(name);
        if (null == param){
            return null;
        }else {
            return param.get(0);
        }
    }
}
